package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public Result addSetmeal(SetmealDTO setmealDTO) {
        //1.非空校验
        if (setmealDTO.getCategoryId() == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.创建实体类,把DTO复制给实体类
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //3.封装数据,调用mapper
        setmealMapper.insertSetmeal(setmeal);

        //新增菜品集合(遍历)
        //拿到菜品集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //遍历添加菜品
        if (setmealDishes ==null && setmealDishes.size()==0){
            return Result.error(MessageConstant.TYPE_NOT_FOUND);
        }
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.addSetmeal(setmealDishes);

        //4.返回数据
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.非空判断
        if (setmealPageQueryDTO.getPage() == null){
            setmealPageQueryDTO.setPage(1);
        }
        if (setmealPageQueryDTO.getPageSize() == null){
            setmealPageQueryDTO.setPageSize(5);
        }
        //2.开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //3.封装数据
        Page<Setmeal> setmaelpage = setmealMapper.page(setmealPageQueryDTO);
        //4.返回数据
        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmaelpage.getTotal());
        pageResult.setRecords(setmaelpage.getResult());

        return Result.success(pageResult);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @Override
    public Result delectSetmeal(List<Long> ids) {
        //1.非空校验
        if (ids == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.判断套餐是否存在
        for (Long id : ids) {
            //根据id查询套餐是否存在
            Setmeal setmeal = setmealMapper.geyById(id);
            //如果存在,是起售状态则不能删除
            if (setmeal != null && setmeal.getStatus() == StatusConstant.ENABLE){
                    return Result.error(MessageConstant.SETMEAL_ON_SALE);
            }
            setmealMapper.deleteSetmealId(id);
            setmealDishMapper.deleteSetmealDishID(id);
        }



        //4.返回数据
        return Result.success();
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @Override
    public Result updateSetmeal(SetmealDTO setmealDTO) {
        //1.非空校验(判断id是否存在)
        if (setmealDTO.getCategoryId() == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }

        //2.创建实体类并修改信息
            //创建实体类
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
            //修改信息
        setmealMapper.update(setmeal);

        //3.删除菜品表
        dishMapper.deleteDish(Arrays.asList(setmeal.getId()));

        //4.新增菜品表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setDishId(setmeal.getId()));

//        //2.判断套餐是否存在,如果存在状态为起售则不能删除
//        if (setmealDTO.getCategoryId() != null && setmealDTO.getStatus() == StatusConstant.ENABLE){
//            return Result.error(MessageConstant.SETMEAL_ON_UPDATE);
//        }

//        dishMapper.insertDish(setmealDishes);

        return Result.success();
    }

    /**
     * 根据id回显数据
     *
     * @param id
     * @return
     */
    @Override
    public Result selectId(Long id) {
        //1.非空校验
        if(id == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }

        //2.创建实体类对象,根据id回显数据
        Setmeal setmeal = setmealMapper.getById(id);

        //根据id查询菜品表的数据
        List<SetmealDish> dishById = setmealDishMapper.getDishBySetmealId(setmeal.getId());


        //将查询到的数据封装到vo
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(dishById);

        return Result.success(setmealVO);
    }

    /**
     * 起售停售
     *
     * @param status
     * @return
     */
    @Override
    public Result status(Integer status,Long id) {
        //1.非空校验(判断id是否存在)
        if(id == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.修改状态,传参给mapper
            //实例化对象
        Setmeal setmeal = new Setmeal();
            //传入状态、id、修改时间、修改人
        //获取要修改套餐的状态,并赋值
        setmeal.setStatus(status);
        //获取要修该套餐的id
        setmeal.setId(id);
        //获取当前时间,并赋值
        setmeal.setUpdateTime(LocalDateTime.now());
        //获取修改人的id,并赋值
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        //调用mapper,传入数据库
        setmealMapper.update(setmeal);

        //3.返回数据
        return Result.success();
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
